Stygian = Stygian or {}
Stygian.calendar = Stygian.calendar or {}
Stygian.calendar.fontSize = 9
Stygian.calendar.highlight = Stygian.calendar.highlight or "<purple>"
Stygian.calendar.color = Stygian.calendar.color or "<green>"
Stygian.calendar.herbs = Stygian.calendar.herbs or true
Stygian.date = Stygian.date or {}
Stygian.calendar.display = Stygian.calendar.display or {}
Stygian.calendar.display[1] = string.format("%s%s01%s  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[2] = string.format("%s01  %s02%s  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[3] = string.format("%s01  02  %s03%s  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[4] = string.format("%s01  02  03  %s04%s  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[5] = string.format("%s01  02  03  04  %s05\n%s06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[6] = string.format("%s01  02  03  04  05\n%s06%s  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[7] = string.format("%s01  02  03  04  05\n06  %s07%s  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[8] = string.format("%s01  02  03  04  05\n06  07  %s08%s  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[9] = string.format("%s01  02  03  04  05\n06  07  08  %s09%s  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[10] = string.format("%s01  02  03  04  05\n06  07  08  09  %s10\n%s11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[11] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n%s11%s  12  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[12] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  %s12%s  13  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[13] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  %s13%s  14  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[14] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  %s14%s  15\n16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[15] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  %s15\n%s16  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[16] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n%s16%s  17  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[17] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  %s17%s  18  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[18] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  %s18%s  19  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[19] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  %s19%s  20\n21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[20] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  %s20\n%s21  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[21] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n%s21%s  22  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[22] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  %s22%s  23  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[23] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  %s23%s  24  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[24] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  %s24%s  25",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)
Stygian.calendar.display[25] = string.format("%s01  02  03  04  05\n06  07  08  09  10\n11  12  13  14  15\n16  17  18  19  20\n21  22  23  24  %s25%s",Stygian.calendar.color, Stygian.calendar.highlight, Stygian.calendar.color)

function Stygian_calendarPosX()
  local WindowWidth, WindowHeight = getMainWindowSize()
  return 0
end

function Stygian_calendarPosY()
  local WindowWidth, WindowHeight = getMainWindowSize()
  return 0
end
function Stygian_createCalendar()
  local WindowWidth, WindowHeight = getMainWindowSize()
  local fontx,fonty = calcFontSize(Stygian.calendar.fontSize)
  setMiniConsoleFontSize("StygianCalendar", Stygian.calendar.fontSize)
  createMiniConsole("StygianCalendar", 0, 0, 0, 0)
  Stygian.calendar.width = fontx * 20
  Stygian.calendar.height = fonty * 8
  setBackgroundColor("StygianCalendar", 0, 0, 0, 0)
  resizeWindow("StygianCalendar", Stygian.calendar.width, Stygian.calendar.height) 
  moveWindow("StygianCalendar", Stygian_calendarPosX(), Stygian_calendarPosY() )
  if Stygian.calendar.herbs then Stygian_calendarHerbWindowCreate() end
end

function Stygian_resizeCalendar()
  local WindowWidth, WindowHeight = getMainWindowSize()
  local fontx,fonty = calcFontSize(Stygian.calendar.fontSize)
  setMiniConsoleFontSize("StygianCalendar", Stygian.calendar.fontSize)
  Stygian.calendar.width = fontx * 20
  Stygian.calendar.height = fonty * 7
  resizeWindow("StygianCalendar", Stygian.calendar.width, Stygian.calendar.height) 
  moveWindow("StygianCalendar", Stygian_calendarPosX(), Stygian_calendarPosY() )
  if Stygian.calendar.herbs then Stygian_calendarHerbWindowResize() end
end

function Stygian_displayCalendar()
  SDebug("calendar", "We're gonna show the calendar now")
  clearWindow("StygianCalendar")
  local header = Stygian.date.month .. " " .. Stygian.date.year
  local pad = math.floor((20 - string.len(header)) / 2)
  local time = align(string.upper(Stygian.date.time or "unknown"), {textColor = "<red>", width = 20,})  
  pad = string.rep(" ", pad)
  header = pad .. "<purple>" .. header
  pad = math.floor((20 - string.len(Stygian.date.moonphase)) / 2)
  pad = string.rep(" ", pad)
  local moonphase = pad .. "<purple>" .. Stygian.date.moonphase
  cecho("StygianCalendar", string.format("%s\n%s\n%s\n%s",time, header, moonphase, Stygian.calendar.display[Stygian.date.day]))
  if Stygian.calendar.herbs then Stygian_calendarDisplayHerbWindow() end
end

Stygian.calendar.herbsHibernating = { Estar = "flax merbloom weed",
                                        Urlachmar = "kafe rawtea",
                                        Kiani = "colewort",
                                        Dioni = "calamus earwort juniper sage",
                                        Vestian = "horehound wormwood",
                                        Avechary = "mistletoe",
                                        Dvarsh = "sparkleberry",
                                        Tzarin = "reishi sargassum",
                                        Klangiary = "arnica pennyroyal",
                                        Juliary = "coltsfoot faeleaf",
                                        Shanthin = "galingale marjoram rosehips",
                                        Roarkian = "chervil yarrow",
}
Stygian.calendar.herbsFlourishing = { Dvarsh = "flax merbloom weed",
                                        Tzarin = "kafe rawtea",
                                        Klangiary = "colewort",
                                        Juliary = "calamus earwort juniper sage",
                                        Shanthin = "horehound wormwood",
                                        Avechary = "chervil yarrow",
                                        Estar = "sparkleberry",
                                        Urlachmar = "reishi sargassum",
                                        Kiani = "arnica pennyroyal",
                                        Dioni = "coltsfoot faeleaf",
                                        Vestian = "galingale marjoram rosehips",
                                        Avechary = "chervil yarrow",
                                        Roarkian = "mistletoe"
}
Stygian.calendar.herbsReplant = { Urlachmar = "flax merbloom weed",
                                    Kiani = "kafe rawtea",
                                    Dioni = "colewort",
                                    Vestian = "calamus earwort juniper sage",
                                    Avechary = "horehound wormwood",
                                    Dvarsh = "mistletoe",
                                    Tzarin = "sparkleberry",
                                    Klangiary = "reishi sargassum",
                                    Juliary = "arnica pennyroyal",
                                    Shanthin = "coltsfoot faeleaf",
                                    Roarkian = "galingale marjoram rosehips",
                                    Estar = "chervil yarrow",
}


function Stygian_calendarHerbWindowCreate()
  createMiniConsole("Stygian_calendar_herb", 0, 0, 0, 0)
  local WindowWidth, WindowHeight = getMainWindowSize()
  local fontx,fonty = calcFontSize(Stygian.calendar.fontSize)
  setMiniConsoleFontSize("Stygian_calendar_herb", Stygian.calendar.fontSize)
  Stygian.calendar.herbWidth = fontx * 45
  Stygian.calendar.herbHeight = fonty * 3
  setBackgroundColor("Stygian_calendar_herb", 0, 0, 0, 0)
  resizeWindow("Stygian_calendar_herb", Stygian.calendar.herbWidth, Stygian.calendar.herbHeight) 
  moveWindow("Stygian_calendar_herb", 0, Stygian_calendarPosY() + Stygian.calendar.height )
end

function Stygian_calendarHerbWindowResize()
  local WindowWidth, WindowHeight = getMainWindowSize()
  local fontx,fonty = calcFontSize(Stygian.calendar.fontSize)
  setMiniConsoleFontSize("Stygian_calendar_herb", Stygian.calendar.fontSize)
  Stygian.calendar.herbWidth = fontx * 45
  Stygian.calendar.herbHeight = fonty * 3
  setBackgroundColor("Stygian_calendar_herb", 0, 0, 0, 0)
  resizeWindow("Stygian_calendar_herb", Stygian.calendar.herbWidth, Stygian.calendar.herbHeight) 
  moveWindow("Stygian_calendar_herb", WindowWidth - Stygian.calendar.herbWidth, Stygian_calendarPosY() + Stygian_calender_height )
end

function Stygian_calendarDisplayHerbWindow()
  clearWindow("Stygian_calendar_herb")
  local month = Stygian.date.month
  local herbs_to_replant = Stygian.calendar.herbsReplant[month]
  local herbs_flourishing = Stygian.calendar.herbsFlourishing[month]
  local herbs_hibernating = Stygian.calendar.herbsHibernating[month]
  cecho("Stygian_calendar_herb", "<purple>Flourishing:<green> " .. herbs_flourishing .. "\n<purple>Hibernating:<green> " .. herbs_hibernating .. "\n<purple>to replant: <green>" .. herbs_to_replant)
end
